package wifiunion.com.notice.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import java.util.HashMap;

import wifiunion.com.notice.BaseFragment;
import wifiunion.com.notice.BasePresenter;
import wifiunion.com.notice.ConfigureContact;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.User;
import wifiunion.com.notice.presenter.AddConfigurePresenter;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * 配置首屏
 * Created by Administrator on 2016/11/23.
 */

public class NoticeConfigueFragment extends BaseFragment implements View.OnClickListener, ConfigureContact.View {

    private View mMainView;


    private User mUser;


    private EditText mSingleNoticeEdt, mMutipleNoticeEdt, mDoubleNoticeEdt;


    private TextView mNoticeConfigueSaveTv;

    private AddConfigurePresenter mPresenter;

    private View mProgressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMainView = inflater.inflate(R.layout.fragment_noticeconfigue, container, false);
        return mMainView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        mUser = SharedPreferencesUtils.getUserSharedPreferences(getContext());


        mSingleNoticeEdt = (EditText) mMainView.findViewById(R.id.singlenotice_edt);
        mMutipleNoticeEdt = (EditText) mMainView.findViewById(R.id.multiplenotice_edt);
        mDoubleNoticeEdt = (EditText) mMainView.findViewById(R.id.doublenotice_edt);

        mNoticeConfigueSaveTv = (TextView) mMainView.findViewById(R.id.noticeconfigue_comfirm_tv);
        mProgressBar = mMainView.findViewById(R.id.loading_progress);
        initPeroidValue();

        mNoticeConfigueSaveTv.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.noticeconfigue_comfirm_tv) {
            if (isPeriodParamCorrect()) {
                setPeriodParam();

                try {
                    mPresenter = new AddConfigurePresenter(getContext(), this, 9);
                    HashMap<String, Object> params = new HashMap<>();
                    String authtoken = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_AUTHTOKEN, getContext());

                    params.put("Authorization", authtoken);
                    params.put("controlTime", (Integer.parseInt(mDoubleNoticeEdt.getEditableText().toString()) / 1000));
                    mPresenter.updateControlTime(params);

                    onProgress();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }


    @Override
    public void initview() {

    }

    @Override
    public void initData() {


    }

    /**
     * 初始化间隔时间
     */
    private void initPeroidValue() {
        String singleStr = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_SINGLENOTICE_PEROID, getContext());

        String mutipleStr = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_MULTIPLENOTICE_PEROID, getContext());


        if (TextUtils.isEmpty(singleStr)) {
            singleStr = "6000";
            mSingleNoticeEdt.setText(singleStr);
            mSingleNoticeEdt.setSelection(singleStr.length());
            SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_SINGLENOTICE_PEROID, "6", getContext());
        } else {
            try {
                Double singleD = Double.parseDouble(singleStr);

                int singleint = (int) (singleD * 1000);
                mSingleNoticeEdt.setText(String.valueOf(singleint));
                mSingleNoticeEdt.setSelection(String.valueOf(singleint).length());

            } catch (NumberFormatException e) {
                e.printStackTrace();
                singleStr = "6000";
                mSingleNoticeEdt.setText(singleStr);
                mSingleNoticeEdt.setSelection(singleStr.length());
                SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_SINGLENOTICE_PEROID, "6", getContext());
            }
        }


        if (TextUtils.isEmpty(mutipleStr)) {
            mutipleStr = "2000";
            mMutipleNoticeEdt.setText(mutipleStr);
            mMutipleNoticeEdt.setSelection(mutipleStr.length());
            SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_MULTIPLENOTICE_PEROID, "2", getContext());
        } else {
            try {
                Double mutipleD = Double.parseDouble(mutipleStr);

                int mutipleint = (int) (mutipleD * 1000);
                mMutipleNoticeEdt.setText(String.valueOf(mutipleint));
                mMutipleNoticeEdt.setSelection(String.valueOf(mutipleint).length());

            } catch (NumberFormatException e) {
                e.printStackTrace();
                mutipleStr = "2000";
                mMutipleNoticeEdt.setText(mutipleStr);
                mMutipleNoticeEdt.setSelection(mutipleStr.length());
                SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_MULTIPLENOTICE_PEROID, "2", getContext());
            }
        }

//        try {
//            mDoubleNoticeEdt.setText(String.valueOf(mUser.getControlTime()));
//        } catch (Exception e) {
//            mDoubleNoticeEdt.setText("0");
//            e.printStackTrace();
//        }
        try {
            mDoubleNoticeEdt.setText(String.valueOf(mUser.getControlTime() * 1000));
        } catch (Exception e) {
            mDoubleNoticeEdt.setText("0");
            e.printStackTrace();
        }
    }

    /**
     * 设置通知间隔时间
     */
    private void setPeriodParam() {

        int singleTimeint = Integer.parseInt(mSingleNoticeEdt.getText().toString());
        int mutipleTimeint = Integer.parseInt(mMutipleNoticeEdt.getText().toString());

        double singleTimeD = singleTimeint * 1.000d / 1000;
        double mutipleTimeD = mutipleTimeint * 1.000d / 1000;

        SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_SINGLENOTICE_PEROID, String.valueOf(singleTimeD), getContext());
        SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_MULTIPLENOTICE_PEROID, String.valueOf(mutipleTimeD), getContext());

    }

    /**
     * 检测设置时间是否合理
     *
     * @return
     */
    private boolean isPeriodParamCorrect() {

        String singlePeriod = mSingleNoticeEdt.getText().toString();
        String mutiplePeriod = mMutipleNoticeEdt.getText().toString();

        String doublePeriod = mDoubleNoticeEdt.getText().toString();


        if (TextUtils.isEmpty(singlePeriod) || TextUtils.isEmpty(mutiplePeriod) || TextUtils.isEmpty(doublePeriod)) {
            Toast.makeText(getContext(), "配置参数请输入完整", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            int singleTime = Integer.parseInt(singlePeriod);
            int mutipleTime = Integer.parseInt(mutiplePeriod);
            if (singleTime <= 0) {
                Toast.makeText(getContext(), "单条停留时间须大于0", Toast.LENGTH_SHORT).show();
                return false;
            } else if (mutipleTime <= 0) {
                Toast.makeText(getContext(), "多条间隔时间须大于0", Toast.LENGTH_SHORT).show();
                return false;
            } else if (singleTime < mutipleTime) {
                Toast.makeText(getContext(), "单条停留时间不得小于多条间隔时间", Toast.LENGTH_SHORT).show();
                return false;
            } else {
                return true;
            }
        }
    }


    public void updateControlTime() {

    }

    @Override
    public void onProgress() {

        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgressBar.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    @Override
    public void onError(String str) {

    }

    @Override
    public void onSuccess(Object obj) {

    }

    @Override
    public void onImgUploadSuccess(String imgpath) {

    }

    @Override
    public void onAddCofigureSuccess() {

    }

    @Override
    public void onEditConfigureSuccess() {

    }

    @Override
    public void onUpdateNoticeConfigureSuccess() {

    }

    @Override
    public void onUpdateNoticeConfigureFailed(Object obj) {

    }

    @Override
    public void onUpdatePlaceConfigureSuccess() {

    }

    @Override
    public void onUpdatePlaceConfigureFailed(Object obj) {

    }

    @Override
    public void onUpdateStrangerConfigureSuccess() {

    }

    @Override
    public void onUpdateStrangerConfigureFailed(Object obj) {

    }

    @Override
    public void onUpdateFZPZConfigureSuccess() {

    }

    @Override
    public void onUpdateFZPZConfigureFailed(Object obj) {

    }

    @Override
    public void getStrangerConfigueSuccess(Object obj) {

    }

    @Override
    public void getStrangerConfigueFailed(Object obj) {

    }

    @Override
    public void getDeviceListSuccess(Object obj) {

    }

    @Override
    public void getDeviceListFailed(Object obj) {

    }

    @Override
    public void updateControltimeSuccess(Object obj) {

        if (getActivity() != null) {
            mProgressBar.setVisibility(View.GONE);
            Toast.makeText(getContext(), "设置成功", Toast.LENGTH_SHORT).show();

            //更新内存
            mUser.setControlTime(Integer.parseInt(mDoubleNoticeEdt.getEditableText().toString()) / 1000);
            SharedPreferencesUtils.setPlaceUserSharedPreferences(mUser,getContext());
            SharedPreferencesUtils.setUserSharedPreferences(mUser,getContext());

        }
    }

    @Override
    public void updateControltimeFailed(final Object obj) {
        if (getActivity() != null) {
            mProgressBar.setVisibility(View.GONE);
            if (obj != null) {
                Toast.makeText(getContext(), (String) obj, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "设置失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void setPresenter(BasePresenter tp) {

    }
}
